package com.emip.security;

import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.emip.bean.UserBean;
import com.emip.service.MenuDefService;
import com.emip.service.UserService;
import com.emip.service.impl.UserServiceImpl;

public class CheckAccountServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;   
	
    public void doGet(HttpServletRequest request, HttpServletResponse response)     
            throws ServletException, IOException {     
     
        doPost(request, response);     
    }     
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)     
    throws ServletException, IOException {  
    	response.setContentType("text/html;charset=utf-8");
    	String username = request.getParameter("username");
    	String userpwd = request.getParameter("password");
    	PrintWriter out = response.getWriter();   
    	
    	//ApplicationContext appContext = new ClassPathXmlApplicationContext("config/applicationContext.xml");
    	WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    	UserService userService = (UserService)appContext.getBean("userService");
    	    	
    	UserBean user = userService.login(username, userpwd);
    	if (user!= null) {
    		request.getSession().setAttribute("user", user);
    		out.print("accountValid");
    	}
    	else {
    		out.print("accountInvalid");
    	}
    		
    }
}
